package com.kaili.basic.controller.web.v1;

import com.kaili.basic.pojo.dto.BaseRouteLineAddDto;
import com.kaili.basic.pojo.dto.BaseRouteLineUpdateDto;
import com.kaili.basic.pojo.entity.BaseRouteLine;
import com.kaili.basic.pojo.entity.BaseRouteMenu;
import com.kaili.basic.pojo.entity.BasicSite;
import com.kaili.basic.service.BaseRouteLineService;
import com.kaili.basic.service.BasicSiteService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangjl
 * @Type BaseRouteLineController
 * @Desc 杭州凯立通信有限公司
 * @Date 2018年05月30日
 */
@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "专线路由", description = "专线路由相关API")
public class BaseRouteLineController {

    @Autowired
    private BaseRouteLineService baseRouteLineService;
    @Autowired
    private BasicSiteService basicSiteService;

    /**
     * 新增专线路由
     *
     * @param baseRouteLine
     * @return
     */
    @PostMapping("/routeLines")
    @ApiOperation(value = "新增专线路由", notes = "新增专线路由")
    public RestStatus addRouteLine(@Validated @RequestBody BaseRouteLineAddDto BaseRouteLineAddDto) {
        BaseRouteLine baseRouteLine = new BaseRouteLine();
        BeanUtils.copyProperties(BaseRouteLineAddDto, baseRouteLine);
        BasicSite basicSite1 = basicSiteService.getBySiteId(baseRouteLine.getStartSite());
        BasicSite basicSite2 = basicSiteService.getBySiteId(baseRouteLine.getReachSite());
//        String str =StringUtils.substringBeforeLast(basicSite.getSiteName(),"网点")+"专线";
        String str = basicSite1.getSiteName()+"--"+basicSite2.getSiteName()+"专线";
        BaseRouteMenu baseRouteMenu = baseRouteLineService.getByMenuName(str);
        //专线路由菜单新增
        if (baseRouteMenu == null){
            BaseRouteMenu baseRouteMenu1 = new BaseRouteMenu();
            baseRouteMenu1.setRouteMenuName(str);
            baseRouteLineService.addRouteMenu(baseRouteMenu1);
        }
        BaseRouteMenu baseRouteMenu2 = baseRouteLineService.getByMenuName(str);
        baseRouteLine.setRouteMenuId(baseRouteMenu2.getId());
        /** 校验专线路由重复 **/
        Map<String, Object> map = new HashMap();
        map.put("startSite", baseRouteLine.getStartSite());
        if (baseRouteLine.getWaySiteOne() != null) {
            map.put("waySiteOne", baseRouteLine.getWaySiteOne());
        } else {
            map.put("waySiteOne", 0);
            baseRouteLine.setWaySiteOne(0L);
        }
        if (baseRouteLine.getWaySiteTwo() != null) {
            map.put("waySiteTwo", baseRouteLine.getWaySiteTwo());
        } else {
            map.put("waySiteTwo", 0);
            baseRouteLine.setWaySiteTwo(0L);
        }
        if (baseRouteLine.getWaySiteThree() != null) {
            map.put("waySiteThree", baseRouteLine.getWaySiteThree());
        } else {
            map.put("waySiteThree", 0);
            baseRouteLine.setWaySiteThree(0L);
        }
        map.put("reachSite", baseRouteLine.getReachSite());
        List<BaseRouteLine> lineMap = baseRouteLineService.queryRouteLineMap(map);
        if (lineMap.size() > 0){
                return CommonStatus.ROUTE_LINE_REPEAT;
        }
        boolean flag = baseRouteLineService.addRouteLine(baseRouteLine);
        if (flag) {
            return CommonStatus.ADD_OK;
        } else {
            return CommonStatus.ADD_FAIL;
        }
    }

    /**
     * 修改专线路由
     *
     * @param baseRouteLine
     * @return
     */
    @PutMapping("/routeLines")
    @ApiOperation(value = "修改专线路由", notes = "修改专线路由")
    public RestStatus updateRouteLine(@Validated @RequestBody BaseRouteLineUpdateDto baseRouteLineUpdatDto) {
        BaseRouteLine baseRouteLine = new BaseRouteLine();
        BeanUtils.copyProperties(baseRouteLineUpdatDto, baseRouteLine);
        BasicSite basicSite = basicSiteService.getBySiteId(baseRouteLine.getReachSite());
        String str =basicSite.getSiteName()+"专线";
        BaseRouteMenu baseRouteMenu = baseRouteLineService.getByMenuName(str);
        //专线路由菜单新增
        if (baseRouteMenu == null){
            BaseRouteMenu baseRouteMenu1 = new BaseRouteMenu();
            baseRouteMenu1.setRouteMenuName(str);
            baseRouteLineService.addRouteMenu(baseRouteMenu1);
        }
        BaseRouteMenu baseRouteMenu2 = baseRouteLineService.getByMenuName(str);
        baseRouteLine.setRouteMenuId(baseRouteMenu2.getId());

        /** 校验专线路由重复 **/
        Map<String, Object> map = new HashMap();
        map.put("startSite", baseRouteLine.getStartSite());
        if (baseRouteLine.getWaySiteOne() != null) {
            map.put("waySiteOne", baseRouteLine.getWaySiteOne());
        } else {
            map.put("waySiteOne", 0);
            baseRouteLine.setWaySiteOne(0L);
        }
        if (baseRouteLine.getWaySiteTwo() != null) {
            map.put("waySiteTwo", baseRouteLine.getWaySiteTwo());
        } else {
            map.put("waySiteTwo", 0);
            baseRouteLine.setWaySiteTwo(0L);
        }
        if (baseRouteLine.getWaySiteThree() != null) {
            map.put("waySiteThree", baseRouteLine.getWaySiteThree());
        } else {
            map.put("waySiteThree", 0);
            baseRouteLine.setWaySiteThree(0L);
        }
        map.put("reachSite", baseRouteLine.getReachSite());
        List<BaseRouteLine> lineMap = baseRouteLineService.queryRouteLineMap(map);
        if (lineMap != null && lineMap.size() >0) {
            BaseRouteLine entity = lineMap.get(0);
            if (baseRouteLine.getRouteId() != entity.getRouteId()) {
                if (lineMap.size() > 0) {
                    return CommonStatus.ROUTE_LINE_REPEAT;
                }
            }
        }
        int flag = baseRouteLineService.updateRouteLine(baseRouteLine);
        //查找到记录，并修改成功
        if (flag > 0) {
            return CommonStatus.UPDATE_OK;
        } else {
            return CommonStatus.UPDATE_FAIL;
        }
    }

    /**
     * 删除专线路由
     *
     * @param baseRouteLine1
     * @return
     */
    @DeleteMapping("/routeLines/{routeId}")
    @ApiOperation(value = "删除专线路由", notes = "删除专线路由")
    public RestStatus delete(@RequestParam("routeId") Integer routeId, @RequestParam("parentId") Integer parentId) {
        BaseRouteLine baseRouteLine = baseRouteLineService.getByRouteId(routeId);
        if (baseRouteLine != null) {
            baseRouteLine.setRdStatus(0);
            baseRouteLineService.updateRouteLine(baseRouteLine);
        }
        List<BaseRouteLine> routeLineList = baseRouteLineService.getRouteLineList(parentId);
        if (routeLineList != null && routeLineList.size()==0){
            BaseRouteMenu baseRouteMenu = new BaseRouteMenu();
            baseRouteMenu.setId(parentId);
            baseRouteLineService.deleteRouteMenu(baseRouteMenu.getId());
        }
        return CommonStatus.DELETE_OK;
    }

    /**
     * 查询专线路由列表
     * @param
     * @return
     */
    @GetMapping("/routeLines")
    @ApiOperation(value = "查询专线路由列表", notes = "查询专线路由列表")
    public List<BaseRouteLine> listRouteLine() {
        return baseRouteLineService.listRouteLine();
    }
}
